package com.gang.ssm.bos.system.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.gang.ssm.bos.core.constant.Const;
import com.gang.ssm.bos.core.constant.ConstShiro;
import com.gang.ssm.bos.core.exception.NoPermissionException;
import com.gang.ssm.bos.core.exception.NoUserException;
import com.gang.ssm.bos.core.toolbox.ajax.AjaxResult;
import com.gang.ssm.bos.core.toolbox.file.BladeFile;
import com.gang.ssm.bos.core.toolbox.kit.StrKit;
import com.gang.ssm.bos.core.toolbox.support.WafRequestWrapper;


public class BosController implements ConstShiro {
	
	@Resource
	private HttpServletRequest request;
	
	protected HttpServletRequest getRequest() {
		return new WafRequestWrapper(this.request);
	}
	
	public AjaxResult json(Object data) {
		return new AjaxResult().success(data);
	}
	
	public AjaxResult json(Object data, String message) {
		return json(data).setMessage(message);
	}
	
	
	public AjaxResult json(Object data, String message, int code) {
		return json(data, message).setCode(code);
	}
	
	
	public AjaxResult success(String message) {
		return new AjaxResult().addSuccess(message);
	}
	
	public AjaxResult success(Object data) {
		return new AjaxResult().success(data);
	}
	
	public AjaxResult success() {
		return new AjaxResult();
	}
	
	public AjaxResult fail() {
		return new AjaxResult();
	}
	
	
	public AjaxResult error(String message) {
		return new AjaxResult().addError(message);
	}
	
	
	public AjaxResult warn(String message) {
		return new AjaxResult().addWarn(message);
	}
	
	
	public AjaxResult fail(String message) {
		return new AjaxResult().addFail(message);
	}
	
	public boolean isAjax() {
		String header = getRequest().getHeader("X-Requested-With");
		boolean isAjax = "XMLHttpRequest".equalsIgnoreCase(header);
		return isAjax;
	}
	
	public boolean isPost() {
		String method = getRequest().getMethod();
		return StrKit.equalsIgnoreCase("POST", method);
	}
	
	public BladeFile getFile(MultipartFile file){
		return BladeFile.getFile(file);
	}
	
	@ResponseBody
	@ExceptionHandler(Exception.class)
	public Object exceptionHandler(Exception ex, HttpServletResponse response, HttpServletRequest request) {
		AjaxResult result = new AjaxResult();
		String url = Const.ERROR_500;
		String msg = ex.getMessage();
		Object resultModel = null;
		try {
			if (ex.getClass() == HttpRequestMethodNotSupportedException.class) {
				url = Const.ERROR_500;// 请求方式不允许抛出的异常,后面可自定义页面
			} else if (ex.getClass() == NoPermissionException.class) {
				url = Const.ERROR_500;// 无权限抛出的异常
				msg = ConstShiro.NO_PERMISSION;
			} else if (ex.getClass() == NoUserException.class) {
				url = Const.LOGIN_REALPATH;// session过期抛出的异常
				msg = ConstShiro.NO_USER;
			} else if (ex instanceof MaxUploadSizeExceededException) {
				long maxSize = ((MaxUploadSizeExceededException)ex).getMaxUploadSize();
				url = Const.ERROR_403;
				msg = Const.UPLOAD_SIZE_TO_LONG + maxSize / 1024 + "k";
			}
			if (isAjax() || isPost()) {
				result.addFail(msg);
				resultModel = result;
			} else {
				ModelAndView view = new ModelAndView(url);
				view.addObject("error", msg);
				view.addObject("class", ex.getClass());
				view.addObject("method", request.getRequestURI());
				resultModel = view;
			}
			
			return resultModel;
		} catch (Exception exception) {
			return resultModel;
		} 
	}
}
